{% extends "base.html" %}

{% block content %}
<script type="text/javascript" src="/static/js/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="/static/js/jquery.cookie.min.js"></script>
<script type="text/javascript" src="/static/js/artDialog/jquery.artDialog.js?skin=default"></script>
<script type="text/javascript" src="/static/js/artDialog/plugins/iframeTools.js"></script>
<script>
function verify(form){
	if(form.name.value==""){
		error("Enter the name please!");
		form.name.focus();		
		return false;
	}
	var testemail = /^[\w\.]+@\w+?(\.\w{2,4}?){1,2}/
	if(form.email.value=="" || !testemail.test(form.email.value)){
		error("Email address input error!");
		form.email.focus();		
		return false;
	}
	if(form.pwd.value==""){
		error("Enter the password please!");
		form.pwd.focus();		
		return false;
	}
	if(form.smtp.value==""){
		error("Enter the smtp please!");
		form.smtp.focus();		
		return false;
	}

	return true;
}
function error(msg){
	$('#errormsg').html(msg)
	setTimeout(function(){
		$('#errormsg').empty()
	},3000)
}

$('form#form1').submit(function(){
	if(verify(this)){
		var mid = $('input[name="id"]').val()
		var name = $('input[name="name"]').val()
		var email = $('input[name="email"]').val()
		var pwd = $('input[name="pwd"]').val()
		var smtp = $('input[name="smtp"]').val()
		var status = mid?$('select[name="status"]').val():1
		var group = $('.group').val()
		var daylimit = $('input[name="daylimit"]').val()
		var data = {'name':name,'email':$('input[name="email"]').val(),'pwd':$('input[name="pwd"]').val(),'smtp':smtp,'status':status,'id':mid,'group':group,'daylimit':daylimit}
		$.post('/savesysemail/',data,function(e){
			e = JSON.parse(e)
			if(e['code']==1){
				error('<font color="green">Successful operation</font>')
				var top = art.dialog.top
				var target = art.dialog.data('target_obj')
				if(mid){
					//修改
					target = top.$(target).parents('tr').children('td')
					target[1].innerHTML = name
					target[2].innerHTML = email
					target[3].innerHTML = pwd
					target[4].innerHTML = smtp
					target[5].innerHTML = 'Group '+group
					target[9].innerHTML = daylimit
					if(status==1){
						target[6].innerHTML = '<span style="color:green;font-size:14px;">√</span>'
					}else{
						target[6].innerHTML = '<span style="color:red;font-size:14px;">×</span>'
					}
				}else{
					//添加
					top.$(target).children('tbody').prepend('<tr><td></td><td>'+name+'</td><td>'+email+'</td><td>'+pwd+'</td><td>'+smtp+'</td><td>Group '+group+'</td><td><span style="color:green;font-size:14px;">√</span></td><td>0</td><td>0</td><td>'+daylimit+'</td><td></td></tr>')
				}
				var api = art.dialog.open.api;
				api && api.close();
			}else{
				error(e['msg'])
			}

		})	
	}
	return false;
})
</script>
<style>
.left{
	float:left;
	margin-left:15px;
}
.title1{
	float:left;
	width:99%;
	margin:2px;
	padding:1px;
	height:auto !important;
	border:1px solid gray;
}
.title2{
	float:left;
}
.selall{
	color:blue;
	cursor:pointer;
}
.chanel{color:green;cursor:pointer;}

</style>
<table width="100%" border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td align="center">
<form name='form1' id='form1' action="" method='post'>
<table width='100%' class='grid' cellspacing='1' align='center'>
<tr valign='middle' align='left'></tr>
<tr valign='middle' align='left'>
	<td class='odd' width='10%'><font color="red">*</font>Send User:</td>
	<td class='even'><input type="text" name="name" value="{{maillist.emailname}}" /></td>
</tr>
<tr valign='middle' align='left'>
	<td class='odd' width='10%'><font color="red">*</font>Email:</td>
	<td class='even'><input type="text" name="email" value="{{maillist.emailadd}}" /></td>
</tr>
<tr valign='middle' align='left'>
	<td class='odd' width='10%'><font color="red">*</font>PassWord:</td>
	<td class='even'><input type="text" name="pwd" value="{{maillist.emailpwd}}" /></td>
</tr>
<tr valign='middle' align='left'>
	<td class='odd' width='10%'><font color="red">*</font>Group:</td>
	<td class='even'>
		<select name="group" id="g_select" class="group">
			{%for g in groups%}
				<option value='{{g.0}}' {%ifequal g.0 maillist.group%}selected{%endifequal%}>Group {{g.0}}</option>
			{%endfor%}
		</select>
		<input id="g_add" style="width:50px;display:none;" type="text" class='' name="" value="{%if maillist.group%}{{maillist.group}}{%else%}1{%endif%}" />
		<input type="text" name="" value='1' id="add_flag" style="display:none;">
		<a href="/" id="addgroup">新增</a>
		
	</td>
</tr>
<tr valign='middle' align='left'>
	<td class='odd' width='10%'><font color="red">*</font>daylimit:</td>
	<td class='even'><input type="text" name="daylimit" value="{%if maillist.daylimit%}{{maillist.daylimit}}{%else%}100{%endif%}" /></td>
</tr>
<tr valign='middle' align='left'>
	<td class='odd' width='10%'><font color="red">*</font>SmtpServer:</td>
	<td class='even'><input type="text" name="smtp" value="{%if maillist.smtp_server%}{{maillist.smtp_server}}{%else%}smtp.gmail.com{%endif%}" /></td>
</tr>
{% if maillist.id %}
<tr valign='middle' align='left'>
	<td class='odd' width='10%'><font color="red">*</font>Status:</td>
	<td class='even'>
		<select name='status'>
			<option value='0'>invalid</option>
			<option value='1' {%ifequal maillist.status 1%}selected{%endifequal%}>valid</option>
		</select>
	</td>
</tr>
	<input type="hidden" name="id" value="{{maillist.id}}" />
{% endif %}
<tr valign='middle' align='left'><td class='odd'></td>
	<td class='even'>&nbsp;<input type='submit' class='button' name='submit'  id='submit' value=' Submit '  />&nbsp;&nbsp;
	{% if not maillist.id %}
	<input type='reset' class='button' name='reset'  id='reset' value=' Reset ' />
	{% endif%}
	</form>
	</td>
</tr>

<tr valign='middle' align='left'><td class='odd'></td>
<td class='even'><font color="red" id='errormsg'>{% if errormsg %}{{errormsg}}{% endif %}</font></td></tr>
</table>
<script>
$('#addgroup').click(function(){
	if($(this).text()=='新增'){
		$(this).text('选择')
		$('#g_add').show()
		$('#g_add').attr('name','group')
		$('#g_add').addClass('group')
		$('#add_flag').attr('name','add_flag')
		$('#g_select').attr('name','')
		$('#g_select').hide()
		$('#g_select').removeClass('group')
	}else{
		$(this).text('新增')
		$('#g_select').show()
		$('#g_select').attr('name','group')
		$('#g_select').addClass('group')
		$('#add_flag').attr('name','')
		$('#g_add').attr('name','')
		$('#g_add').hide()
		$('#g_add').removeClass('group')
	}
	return false;
})
</script>
{% endblock %}

